package com.emm.yixun.website.controller.phone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.emm.yixun.anotations.UserOperateLog;
import com.emm.yixun.common.enums.FunctionTypeEnum;
import com.emm.yixun.common.enums.ModelTypeEnum;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.CustomerInfo;
import com.emm.yixun.common.model.dto.FollowDto;
import com.emm.yixun.common.model.dto.TelRecordDto;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import com.emm.yixun.customer.service.FollowServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.security.ShiroUtil;


@RestController
@RequestMapping("/phone/dial/")
public class DialController {
	
	@Autowired
	private FollowServiceFacade followServiceFacade;
	
	@Autowired
	private CustomerServiceFacade customerServiceFacade;
	
	 @RequestMapping("call")
	 @UserOperateLog(functionName=FunctionTypeEnum.FUNCTIONTYPE_8,modelName=ModelTypeEnum.MODELTYPE_0,projectKey="projectId")
     public AjaxRes call(String caller,String called,Long terminalModelId,String terminalModel){
    	 AjaxRes ar=new AjaxRes();
    	 try {
    		 User user=ShiroUtil.getCurrentUser();
    		 
    		 
			 //记录通话记录
    		 TelRecordDto trd=new TelRecordDto();
    		 trd.setMerchantId(user.getMerchantId());//商户ID
    		 
    		 trd.setCreateUserId(user.getId());//用户ID
    		 trd.setCaller(caller);//主叫号码
    		 trd.setCalled(called);//被叫号码
    		 trd.setCallType("0");//通话类型:0：呼出；1：呼入
    		 trd.setTerminalType("0");//终端类型:0：PC；1：手机；3:座机
    		 trd.setTerminalModel(terminalModel);//终端操作模块:0：客户；
    		 trd.setTerminalModelId(terminalModelId);
    		 trd.setUserCode(user.getUserCode());//终端模块ID，如客户ID
    		 
    		 
    		 FollowDto fd=new FollowDto();
    		 fd.setMerchantId(user.getMerchantId());
    		 fd.setUserCode(user.getUserCode());
    		 fd.setCreateUserId(user.getId());
    		 
    		 if(terminalModel.equals("0")){
    			 Response<Customer> customerResponse=customerServiceFacade.findById(terminalModelId);
        		 if(!customerResponse.isSuccess() || null==customerResponse.getEntity()){
        			 ar.setFailMsg("客户不存在！");
        			 return ar;
        		 }
        		 Customer customer=customerResponse.getEntity();
        		 trd.setProjectId(customer.getProjectId());//项目ID
        		 trd.setAccessCode(customer.getProjectCode());//项目编码
        		 
        		 fd.setCustomerId(customer.getId());
        		 fd.setProjectId(customer.getProjectId());
        		 fd.setProjectCode(customer.getProjectCode());
        		 fd.setAccessCode(customer.getProjectCode());
    		 }else {
    			 ar.setFailMsg("该模块拨暂不支持拨打电话功能！");
    			 return ar;
    		 }
    		 
    		 Response<FollowDto> response=followServiceFacade.callAndFollow(trd, fd);
    		 if(response.isSuccess()){
    			 ar.setSucceed(response.getEntity().getId());
    		 }else{
    			 ar.setFailMsg(response.getResultMessage());;
    		 }
		} catch (Exception e) {
			ar.setFailMsg("系统错误，请联系管理员");;
		}
    	return ar;
     } 
}
